package net.jimmyme.controller;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import net.jimmyme.util.HibernateUtil;

@Controller
public class MainController {

	@RequestMapping(value="/")
	public ModelAndView indexPage(){
		
		ModelAndView modelAndView = new ModelAndView("index");
		
		return modelAndView;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/json")
	public @ResponseBody List<Object> getTest(){
		
		HibernateUtil util = new HibernateUtil();
		Session session = null;
		Transaction transaction = null;
		List<Object> list = null;
		try {
			session = util.openSession();
			transaction = session.beginTransaction();
			
			StringBuffer stringBuffer = new StringBuffer("select c.courseName,sc.grade "
					+ "from Course as c,SelectCourse as sc "
					+ "where c.courseId=sc.course.courseId");
			
			Query query = session.createQuery(stringBuffer.toString());
			list = (List<Object>)query.list();
			
		} catch (Exception e) {
			e.printStackTrace();
			if (transaction != null) {
				transaction.rollback();
			}
		} finally {
			util.closeSession(session);
		}
		
		System.out.println("-----------2");
		
		return list;
	}
}
